package com.wsz.employees.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wsz.employees.feign.DepartmentClient;
import com.wsz.pojo.Department;
import com.wsz.pojo.Employee;
import com.wsz.employees.service.EmployeeService;
import com.wsz.pojo.R;
import com.wsz.pojo.vo.EmployeesVo;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@AllArgsConstructor
@RestController
@RequestMapping("/employees")
public class EmployeesController {

    private final EmployeeService employeeService;

    private final DepartmentClient departmentClient;

    @GetMapping("/test")
    public String Hello(){
        return "hello emp";
    }

    @DeleteMapping("/")
    public R deleteById(@Param("id") Integer id){
        boolean b = employeeService.removeById(id);
        if(b) return R.ok();
        return R.error().message("删除失败");
    }

    @GetMapping("/")
    public R getEmployees(){
        List<Employee> list = employeeService.list();
        ArrayList<Integer> d_id = (ArrayList<Integer>) list.stream().map(Employee::getDepartmentId).distinct().collect(Collectors.toList());

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", JSON.toJSONString(d_id));//添加到json
        String jsonStr = jsonObject.toJSONString();

        R r = departmentClient.getDepartments(jsonStr);


        if(r.getCode().equals(201)){
            return R.error();
        }

        List<Department> deps = (List<Department>) r.getData().get("deps");

        List<EmployeesVo> employeesVos = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            EmployeesVo ev = new EmployeesVo();
            Employee e = list.get(i);
            BeanUtils.copyProperties(e,ev);
            for (int j = 0; j < deps.size(); j++) {
                String tmp = JSON.toJSONString(deps.get(j));
                Department d = JSON.parseObject(tmp, Department.class);
                if(e.getDepartmentId().equals(d.getId())){
                        ev.setDepartment(d);
                        employeesVos.add(ev);
                        break;
                }
            }
        }
        return R.ok().data("employees",employeesVos);
    }

    @PutMapping("/")
    public R updateEmployee(@RequestBody Employee employee){
        boolean b = employeeService.updateById(employee);
        if(b) return R.ok();
        return R.error().message("修改失败");
    }

    @PostMapping("/")
    public R addEmployee(@RequestBody Employee employee){
        boolean save = employeeService.save(employee);
        if(save) return R.ok();
        else return R.error().message("添加失败");
    }

    @GetMapping("/{id}")
    public R getEmployee(@PathVariable("id") Integer id){
        Employee emp = employeeService.getById(id);
        if(emp == null) return R.error().message("没有该员工");
        return R.ok().data("employee",emp);
    }

    @GetMapping("/ojbk")
    public R test(){
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", JSON.toJSONString(list));//添加到json
        String jsonStr = jsonObject.toJSONString();
        return departmentClient.getDepartments(jsonStr);
    }

}
